/**
 * 学习笔记
 */
var localStorageManagers = require("script/localStorageManagers.js");
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true
});
//plus初始化
mui.plusReady(function() {
	plusReady();
});
//plusReady
function plusReady() {
	//滚动条
	mui('.mui-scroll-wrapper').scroll({
					indicators: false //是否显示滚动条
				});
	//加载课题笔记
	loadNoteDiv();
	//切换课程笔记
	mui('#courselitop').on('tap', '.courseli', clickcourseli);
	//打开课题对应的笔记列表
	mui('.coursediv').on('tap', '.catalogdiv', openNotelist);
	//提交或取消
	mui('.coursediv').on('tap', '.suborres', submitOrReset);
};
//加载笔记列表
function loadNoteDiv() {
	if(mobileFrame.IsNetWorkCanUse()){
		document.getElementById('nettip').style.display='none';
	    ajaxLoadNote();
	}else{
		document.getElementById('nettip').style.display='block';
	}
}
//ajax加载笔记列表
function ajaxLoadNote() {
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + "/mobile/note/getNoteListByStudentid.do";
	var w=plus.nativeUI.showWaiting();
	mui.ajax(url, {
		data: {
			userid: userid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (data == null) return;
			buildNoteDiv(data);
			w.close();
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			w.close();
			console.log(e1);
		}
	});
};
//buildNoteDiv
function buildNoteDiv(data) {
	for (var i = 0; i < data.length; i++) {
		var courselitop = document.getElementById('courselitop');
		var courseli = document.getElementById('courseli_' + data[i].courseId);
		//头部列表
		var act = '';
		if (i == 0) act = 'act';
		if (courseli == null) courselitop.innerHTML = courselitop.innerHTML + '<span class="courseli c1 ' + act + '" id="courseli_' + data[i].courseId + '" courseid="' + data[i].courseId + '">' + data[i].courseName + '</span>';
		//中间列表
		var coursediv = document.getElementById('coursediv');
		var catalogsdiv = document.getElementById('catalogsdiv_' + data[i].courseId);
		if (catalogsdiv == null) {
			coursediv.innerHTML = coursediv.innerHTML + '<div class="catalogsdiv ' + act + '" id="catalogsdiv_' + data[i].courseId + '"></div>';
			catalogsdiv = document.getElementById('catalogsdiv_' + data[i].courseId);
		}
		var img = '';
		var isNone='';
		if (data[i].viewed != 0) {
			img = 'note_2.png';
		} else {
			if (data[i].status == 0) {
				img = 'note_4.png';
			} else {
				img = 'note_5.png';
			}
		}
		var catalogstr = '<div class="catalogdiv" catalogid="' + data[i].cataLogId + '" catalogname="' + data[i].cataLogName + '" viewed="'+data[i].viewed+'">' + '<div class="r1"><img src="../../images/note_1.png"><span class="c1">' + data[i].courseName + '</span></div>' + '<div class="r2"><span class="c1">课题：' + data[i].cataLogName + '</span><span class="navigate-right"></span><span class="suborres '+isNone+'"  catalogid="' + data[i].cataLogId + '"  status="' + data[i].status + '"  viewed="'+data[i].viewed+'"><img src="../../images/' + img + '"><span></div>' + '<div class="r3"><img src="../../images/note_6.png"><span class="c1">笔记：' + data[i].count + '</span></div>' + '</div>';
		catalogsdiv.innerHTML = catalogsdiv.innerHTML + catalogstr;
	}
}
//切换课程
function clickcourseli() {
	var courselis = document.getElementsByClassName('courseli');
	for (var i = 0; i < courselis.length; i++) {
		courselis[i].className = 'courseli c1';
	}
	this.className = 'courseli c1 act';

	var items = document.getElementsByClassName('catalogsdiv');
	for (var i = 0; i < items.length; i++) {
		items[i].className = 'catalogsdiv';
	}

	var courseid = this.getAttribute('courseid');
	var catalogsdiv = document.getElementById('catalogsdiv_' + courseid);
	catalogsdiv.className = 'catalogsdiv act';
}
//打开笔记列表页面
function openNotelist() {
	var catalogid = this.getAttribute('catalogid');
	var catalogname = this.getAttribute('catalogname');
	var viewed=this.getAttribute('viewed');
	mui.openWindow({
		url: 'notelist.html',
		id: 'selfcenter/note/notelist',
		extras: {
			catalogid: catalogid,
			catalogname: catalogname,
			viewed:viewed
		}
	});
};
//提交或取消提交
function submitOrReset(e) {
	e.stopPropagation();
	var thisspan = this;
	var status = this.getAttribute('status');
	var viewed=this.getAttribute('viewed');
	if(viewed!=0)return;
	var tishi = '您确定要提交当前笔记吗?';
	if (status == 1) tishi = '您确定要取消提交当前笔记吗';
	var ifchuli = confirm(tishi);
	if (!ifchuli) return;
	var catalogid = this.getAttribute('catalogid');
	var userid = localStorageManagers.getUseridSession();
	var url = serverAjax + "/mobile/note/submitOrResetNote.do";
	mui.ajax(url, {
		data: {
			type: status,
			userid: userid,
			catalogid: catalogid
		},
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			if (true) {
				if (status == 0) {
					thisspan.setAttribute('status', 1);
					thisspan.innerHTML = '<img src="../../images/note_5.png">';
				} else if (status == 1) {
					thisspan.setAttribute('status', 0);
					thisspan.innerHTML = '<img src="../../images/note_4.png">';
				}
			}
			//loadNoteDiv();
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			console.log(e1);
		}
	});

}